package com.gugee.tiktok.web.controller;


import com.gugee.common.web.util.Ret;
import com.gugee.common.web.config.exception.ParamsException;
import com.gugee.tiktok.web.dto.CollectSearchData;
import com.gugee.tiktok.web.model.mysql.AwemeCollection;
import com.gugee.tiktok.web.model.mysql.BloggerCollection;
import com.gugee.tiktok.web.model.mysql.ChallengeCollection;
import com.gugee.tiktok.web.model.mysql.MusicCollection;
import com.gugee.tiktok.web.service.CollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;


@RestController
@Api(value = "收藏",tags = {"收藏"})
public class CollectController extends TikTokBaseController {

    @Autowired
    CollectionService collectionService;




    @ApiOperation(value = "获取收藏视频列表",notes = "%7B%22keyWord%22%3A%22a%22%2C%22sort%22%3A1%2C%22pageSize%22%3A10%2C%22page%22%3A1%7D")
    @RequestMapping(value = "collect/aweme", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String getAwemeCollectList(String data) throws UnsupportedEncodingException, ParamsException {
        val searchData = getParam(CollectSearchData.class, URLDecoder.decode(data, "UTF-8"));
        val awemePage = collectionService.getAwemePage(searchData, super.getUser().getId());
        return Ret.pageData(awemePage.getTotalRow(),awemePage.getList());
    }

    @ApiOperation(value = "收藏视频")
    @RequestMapping(value = "collect/aweme", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String collectAweme(@RequestBody String body) throws ParamsException {
        Long awemeId = getParam(Long.class,body,"awemeId");
        AwemeCollection awemeCollection = collectionService.getAwemeCollection(awemeId,getUser().getId());

        if(null != awemeCollection){
            // return Ret.success("已收藏过该视频",awemeCollection.getId());
            return Ret.success("Added to favorite already",awemeCollection.getId());

        }
        awemeCollection = new AwemeCollection();
        awemeCollection.setAwemeId(awemeId);
        awemeCollection.setUserId(super.getUser().getId());
        awemeCollection.setCreateBy(super.getUser().getEmail());
        awemeCollection.setUpdateBy(super.getUser().getEmail());
        Map<String,Object> data = new HashMap<>();
        data.put("collectId",collectionService.addAwemeCollect(awemeCollection));
        // return Ret.success("收藏成功",data);
        return Ret.success("Added Successfully!",data);
    }


    @ApiOperation(value = "取消收藏视频")
    @RequestMapping(value = "collect/aweme", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public String cancelCollectAweme(@RequestBody String body) throws ParamsException {
        Long collectId = getParam(Long.class,body,"collectId");
        collectionService.cancelCollectAweme(collectId);
        // return Ret.success("OK");
        return Ret.success("Remove from favorites Successfully!");
    }

    @ApiOperation(value = "收藏播主")
    @RequestMapping(value = "collect/blogger", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String collectBlogger(@RequestBody String body) throws ParamsException {
        Long bloggerId = getParam(Long.class,body,"bloggerId");
        BloggerCollection bloggerCollection = collectionService.getBloggerCollection(bloggerId,getUser().getId());
        if(null != bloggerCollection){
            // return Ret.success("已收藏过该播主",bloggerCollection.getId());
            return Ret.success("Added to favorite already",bloggerCollection.getId());
        }
        bloggerCollection = new BloggerCollection();
        bloggerCollection.setUserId(super.getUser().getId());
        bloggerCollection.setBloggerId(bloggerId);
        bloggerCollection.setCreateBy(super.getUser().getEmail());
        bloggerCollection.setUpdateBy(super.getUser().getEmail());
        Map<String,Object> data = new HashMap<>();
        data.put("collectId",collectionService.addBloggerCollect(bloggerCollection));
        // return Ret.success("收藏成功",data);
        return Ret.success("Added Successfully!",data);
    }

    @ApiOperation(value = "取消收藏播主")
    @RequestMapping(value = "collect/blogger", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public String cancelCollectBlogger(@RequestBody String body) throws ParamsException {
        Long collectId = getParam(Long.class,body,"collectId");
        collectionService.cancelCollectBlogger(collectId);
        // return Ret.success("OK");
        return Ret.success("Remove from favorites Successfully!");
    }

    @ApiOperation(value = "获取收藏播主列表")
    @RequestMapping(value = "collect/blogger", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String getCollectBloggerList(String data) throws UnsupportedEncodingException, ParamsException {
        val searchData = getParam(CollectSearchData.class, URLDecoder.decode(data, "UTF-8"));
        val bloggerCollectPage = collectionService.getBloggerCollectPage(searchData, super.getUser().getId());
        return Ret.pageData(bloggerCollectPage.getTotalRow(),bloggerCollectPage.getList());
    }




    @ApiOperation(value = "收藏音乐")
    @RequestMapping(value = "collect/music", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String collectMusic(@RequestBody String body) throws ParamsException {
        Long musicId = getParam(Long.class,body,"musicId");
        MusicCollection musicCollection = collectionService.getMusicCollect(musicId,getUser().getId());
        if(null != musicCollection){
            // return Ret.success("已收藏过该播主",bloggerCollection.getId());
            return Ret.success("Added to favorite already",musicCollection.getId());
        }
        musicCollection = new MusicCollection();
        musicCollection.setUserId(super.getUser().getId());
        musicCollection.setMid(musicId);
        musicCollection.setCreateBy(super.getUser().getEmail());
        musicCollection.setUpdateBy(super.getUser().getEmail());
        Map<String,Object> data = new HashMap<>();
        data.put("collectId",collectionService.addMusicCollect(musicCollection));
        // return Ret.success("收藏成功",data);
        return Ret.success("Added Successfully!",data);
    }

    @ApiOperation(value = "取消收藏音乐")
    @RequestMapping(value = "collect/music", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public String cancelCollectMusic(@RequestBody String body) throws ParamsException {
        Long collectId = getParam(Long.class,body,"collectId");
        collectionService.cancelCollectMusic(collectId);
        // return Ret.success("OK");
        return Ret.success("Remove from favorites Successfully!");
    }

    @ApiOperation(value = "获取收藏音乐列表")
    @RequestMapping(value = "collect/music", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String getCollectMusicList(String data) throws UnsupportedEncodingException, ParamsException {
        val searchData = getParam(CollectSearchData.class, URLDecoder.decode(data, "UTF-8"));
        val musicCollectPage = collectionService.getMusicCollectPage(searchData.getPage(),searchData.getPageSize(),searchData.getKeyword(), super.getUser().getId());
        return Ret.pageData(musicCollectPage.getTotalRow(),musicCollectPage.getList());
    }

    @ApiOperation(value = "获取收藏统计")
    @RequestMapping(value = "collect/count", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String getCollectCount(){
        Map<String,Integer> ret = new HashMap<>();
        ret.put("awemeCount",collectionService.getAwemeCollectCount(getUser().getId()));
        ret.put("bloggerCount",collectionService.getBloggerCollectCount(getUser().getId()));
        ret.put("musicCount",collectionService.getMusicCollectCount(getUser().getId()));
        ret.put("hashtagCount",collectionService.getChallengeCount(getUser().getId()));
        return Ret.success("OK",ret);
    }

    @ApiOperation(value = "收藏标签")
    @RequestMapping(value = "collect/hashtag", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String collectChallenge(@RequestBody String body) throws ParamsException {
        Long cid = getParam(Long.class,body,"cid");
        ChallengeCollection challengeCollection = collectionService.getChallengeCollect(cid,getUser().getId());
        if(null != challengeCollection){
            // return Ret.success("已收藏过该播主",bloggerCollection.getId());
            return Ret.success("Added to favorite already",challengeCollection.getId());
        }
        challengeCollection = new ChallengeCollection();
        challengeCollection.setUserId(super.getUser().getId());
        challengeCollection.setCid(cid);
        challengeCollection.setCreateBy(super.getUser().getEmail());
        challengeCollection.setUpdateBy(super.getUser().getEmail());
        Map<String,Object> data = new HashMap<>();
        data.put("collectId",collectionService.addChallengeCollect(challengeCollection));
        // return Ret.success("收藏成功",data);
        return Ret.success("Added Successfully!",data);
    }

    @ApiOperation(value = "取消收藏标签")
    @RequestMapping(value = "collect/hashtag", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public String cancelCollectChallenge(@RequestBody String body) throws ParamsException {
        Long collectId = getParam(Long.class,body,"collectId");
        collectionService.cancelCollectChallenge(collectId);
        // return Ret.success("OK");
        return Ret.success("Remove from favorites Successfully!");
    }

    @ApiOperation(value = "获取收藏标签列表")
    @RequestMapping(value = "collect/hashtag", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String getCollectChallengeList(String data) throws UnsupportedEncodingException, ParamsException {
        val searchData = getParam(CollectSearchData.class, URLDecoder.decode(data, "UTF-8"));
        val challengeCollectPage = collectionService.getChallengeCollectPage(searchData.getPage(),searchData.getPageSize(),searchData.getKeyword(),searchData.getSort(), super.getUser().getId());
        return Ret.pageData(challengeCollectPage.getTotalRow(),challengeCollectPage.getList());
    }
}
